<?php
// connect
require 'connect.php';

// Create database
// if (mysql_query("CREATE DATABASE lyubishchev",$con))
  // {
  // echo "Database created";
  // }
// else
  // {
  // echo "Error creating database: " . mysql_error();
  // }

//Create table in database
mysql_select_db("lyubishchev", $con);
// $sql = "CREATE TABLE Log
// (
// ID int NOT NULL AUTO_INCREMENT, 
// PRIMARY KEY(ID),
// start 	datetime,
// end 	datetime,
// mainclass 	varchar(15),
// subclass	varchar(15),
// description varchar(140)
// )";
// mysql_query($sql,$con);

// insert

$datestring = date("Y-m-d")." ".$_POST[starthour].":".$_POST[startminute].":00";
$starttime=strtotime($datestring);

// methond 2
// $format = 'Y-m-d H:i:s';
// $time = '2014-4-18 21:50:00';
// $starttimeInstence=date_create_from_format($format, $time );
// $starttime=date_timestamp_get($starttimeInstence);
 $endtime = time();

	if ($starttime > $endtime) {
	die('Start time error <a href="index.html">System</a>');
	}
// echo $starttime;


$sql="INSERT INTO Log (start, end, description)
VALUES
(FROM_UNIXTIME($starttime), FROM_UNIXTIME($endtime), '$_POST[description]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
// echo "1 record added";

$result = mysql_query("SELECT * FROM Log");
echo "<table border='1'>";
while($row = mysql_fetch_array($result))
  {
  	$phptimee=strtotime($row['end']);
	$phptimes=strtotime($row['start']);
	$de = $row['description'];
	$id = $row['ID'];
	
	$durtime = $phptimee - $phptimes;
	$durHour = floor($durtime/3600);
	$durMinute = ($durtime/60)%60;
	$hourString = $durHour==0?"":$durHour."h";
	
  echo "<tr><td>No." . $id . "</td> <td>" . date("h:i",$phptimes) . "</td> <td>" .$hourString . $durMinute . "m</td> <td>" . $de . "</td></tr>";
  
  }
  echo "</table>";



// close db
mysql_close($con);

echo "<a href='index.html'>back</a>";

?>